//
//  AppDelegate.m
//  InteractiveNotification
//
//  Created by GuochengLiu on 2017/1/16.
//  Copyright © 2017年 sumavision. All rights reserved.
//

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#import "AppDelegate.h"
#import "Counter.h"
#import "OMCInteraction.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self _registerForNotificationAction];
//    [self _setupLocationNotification];
    return YES;
}

- (void)_registerForNotificationAction {
    
    UIMutableUserNotificationAction *incrementAction = [[UIMutableUserNotificationAction alloc] init];
    incrementAction.identifier = @"INCREMENT_ACTION";
    incrementAction.title = @"Add +1";
    incrementAction.activationMode = UIUserNotificationActivationModeBackground;
    incrementAction.authenticationRequired = YES;
    incrementAction.destructive = NO;
    
    UIMutableUserNotificationAction *decrementAction = [[UIMutableUserNotificationAction alloc] init];
    decrementAction.identifier = @"DECREMENT_ACTION";
    decrementAction.title = @"Sub -1";
    decrementAction.activationMode = UIUserNotificationActivationModeBackground;
    decrementAction.authenticationRequired = YES;
    decrementAction.destructive = NO;
    
    UIMutableUserNotificationAction *resetAction = [[UIMutableUserNotificationAction alloc] init];
    resetAction.identifier = @"RESET_ACTION";
    resetAction.title = @"Reset";
    resetAction.activationMode = UIUserNotificationActivationModeForeground;
    resetAction.destructive = YES;
    
    UIMutableUserNotificationCategory *countCategory = [[UIMutableUserNotificationCategory alloc] init];
    
    countCategory.identifier = @"COUNT_CATEGORY";
    
    [countCategory setActions:@[incrementAction, decrementAction, resetAction] forContext:UIUserNotificationActionContextDefault];
    
    [countCategory setActions:@[incrementAction, decrementAction] forContext:UIUserNotificationActionContextMinimal];
    
    
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:[NSSet setWithObject:countCategory]];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}


- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    if (notificationSettings.types != UIUserNotificationTypeNone) {
        NSLog(@"已获取通知权限");
    } else {
        NSLog(@"用户未允许推送");
        
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"完整体验App需要开启通知" message:@"点击跳转开启通知" preferredStyle:UIAlertControllerStyleAlert];
        
        
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"跳转" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSURL *appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            [[UIApplication sharedApplication] openURL:appSettings];
        }];
        [alertController addAction:action];
        
        alertController.preferredAction = action;
        
        [application.keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"无法获得通知权限");
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark - Handle Notification Action


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"收到一条消息" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"Add +1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[OMCInteraction sharedInstance] pushWithMedia:@"jdifhdifdf"];
    }]];
     
    [alertController addAction:[UIAlertAction actionWithTitle:@"Sub -1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [Counter decrease];
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"Reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [Counter reset];
    }]];
    
    [application.keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    if ([identifier isEqualToString:@"INCREMENT_ACTION"]) {
        [Counter increase];
    } else if ([identifier isEqualToString:@"DECREMENT_ACTION"]) {
        [Counter decrease];
    } else if ([identifier isEqualToString:@"RESET_ACTION"]) {
        [Counter reset];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        completionHandler();
    });
}


- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
    completionHandler();
}



@end

#pragma GCC diagnostic pop
